/*
 * @Author: 李超越 1509572300@qq.com
 * @Date: 2024-07-29 13:31:22
 * @LastEditors: 李超越 1509572300@qq.com
 * @LastEditTime: 2024-07-29 14:24:45
 * @FilePath: /my_git/lesson24_pipe/mypipe/task.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <vector>
#include <iostream>
using namespace std;


typedef void(*func_t)();

class ForkWork
{
public:
    ForkWork()
    {
        func=new func_t[3];
        func={PrintLog,ReloadConf,ConnectMysql};
    }
    
    void PrintLog()
    {
        cout<<"printf log task"<<endl;
    }

    void ReloadConf()
    {
        cout<<"reload conf task"<<endl;
    }

    void ConnectMysql()
    {
        cout<<"connect mysql task"<<endl;
    }
    
    void work()
    {
        while(true)
        {
            uint32_t command_code=0;
            ssize_t n=read(0,&command_code,sizeof(command_code));
            if(n==sizeof(command_code))
            {
                if(command_code >= 3)
                {
                    continue;
                }
                func[command_code]();
            }
            cout<<"I am worker:"<<endl;
            sleep(1);
        }
    }

    ~ForkWork()
    {
        delete[] func;
    }
   
    uint32_t NextTask()
    {
        return rand()%3;
    }
private:
    func_t* func;
};



